import store from '@/store'
export const panesValues = ['人员', '部门', '岗位']
export const replaceTag = ({ path, autoPushLatestView, $router, query, params, name }) => {
	let tags = store.state.viewTags.viewTags
	if (path) {
		let target = tags.find((res) => {
			return new RegExp(path, 'ig').test(res.path)
		})
		if (target) {
			store.commit('removeViewTags', target)
			store.commit('removeIframeList', target)
			store.commit('removeKeepLive', target.name)
			if (autoPushLatestView) {
				const latestView = tags.slice(-1)[0]
				if (latestView) {
					$router.push(latestView)
				} else {
					$router.push('/')
				}
			}
		}
		if (query) {
			$router.push({
				path,
				query: {
					...query
				}
			})
		} else {
			$router.push({
				path
			})
		}
	} else if (name) {
		let target = tags.find((res) => {
			return name === res.name
		})
		if (target) {
			store.commit('removeViewTags', target)
			store.commit('removeIframeList', target)
			store.commit('removeKeepLive', target.name)
			if (autoPushLatestView) {
				const latestView = tags.slice(-1)[0]
				if (latestView) {
					$router.push(latestView)
				} else {
					$router.push('/')
				}
			}
		}
		if (params) {
			$router.push({
				name,
				params: {
					...params
				}
			})
		} else {
			$router.push({
				name
			})
		}
	}
}
export const jumpHandler = ({ userId, router }) => {
	replaceTag({
		path: '/training/growthDetail',
		autoPushLatestView: true,
		$router: router,
		query: {
			userId
		}
	})
	window.parent.postMessage('potalIframeRenderLoadingFinish', '*')
}
/*iframe 嵌套逻辑*/
export const isFrame = () => {
	return window.top !== window.self
}
